package useragent

import (
	"fmt"
	"math/rand"

	"gitee.com/liumou_site/GoUserAgent/devices"
)

// New 创建一个新的UserAgent生成器
func New() *UserAgentGenerator {
	return &UserAgentGenerator{
		deviceTypes:   []DeviceType{Desktop, Mobile, Tablet},
		osList:        []OS{Windows, MacOS, Linux, Android, IOS},
		browsers:      []Browser{Chrome, Firefox, Safari, Edge},
		brands:        devices.GetAllBrands(),
		useDeviceInfo: false,
	}
}

// WithDeviceTypes 设置支持的设备类型
func (u *UserAgentGenerator) WithDeviceTypes(devices ...DeviceType) *UserAgentGenerator {
	u.deviceTypes = devices
	return u
}

// WithOS 设置支持的操作系统类型
func (u *UserAgentGenerator) WithOS(oss ...OS) *UserAgentGenerator {
	u.osList = oss
	return u
}

// WithBrowsers 设置支持的浏览器类型
func (u *UserAgentGenerator) WithBrowsers(browsers ...Browser) *UserAgentGenerator {
	u.browsers = browsers
	return u
}

// WithBrands 设置支持的品牌
func (u *UserAgentGenerator) WithBrands(brands ...devices.DeviceBrand) *UserAgentGenerator {
	u.brands = brands
	u.useDeviceInfo = true
	return u
}

// Generate 生成一个随机UserAgent
func (u *UserAgentGenerator) Generate() string {
	return u.GenerateWithOptions()
}

// GenerateWithOptions 根据选项生成UserAgent
func (u *UserAgentGenerator) GenerateWithOptions(opts ...GenerateOption) string {
	options := &generateOptions{
		deviceTypes:   u.deviceTypes,
		osList:        u.osList,
		browsers:      u.browsers,
		brands:        u.brands,
		models:        make(map[devices.DeviceBrand][]string),
		useDeviceInfo: u.useDeviceInfo,
	}

	for _, opt := range opts {
		opt(options)
	}

	// 如果指定了设备信息，使用设备信息生成
	if options.useDeviceInfo && options.deviceInfo != nil {
		return u.generateWithDeviceInfo(options)
	}

	// 如果指定了型号映射，使用指定的型号
	if len(options.models) > 0 {
		return u.generateWithModels(options)
	}

	// 如果启用了设备品牌支持
	if options.useDeviceInfo && len(options.brands) > 0 {
		return u.generateWithBrand(options)
	}

	// 原有的生成逻辑
	var filteredPatterns []userAgentPattern
	for _, pattern := range userAgentPatterns {
		if containsDeviceType(options.deviceTypes, pattern.device) &&
			containsOS(options.osList, pattern.os) &&
			containsBrowser(options.browsers, pattern.browser) {
			filteredPatterns = append(filteredPatterns, pattern)
		}
	}

	if len(filteredPatterns) == 0 {
		return ""
	}

	selectedPattern := filteredPatterns[rand.Intn(len(filteredPatterns))]
	versions := browserVersions[selectedPattern.browser]
	if len(versions) == 0 {
		return ""
	}

	version := versions[rand.Intn(len(versions))]

	switch selectedPattern.browser {
	case Firefox:
		version2 := versions[rand.Intn(len(versions))]
		return fmt.Sprintf(selectedPattern.template, version, version2)
	case Edge:
		version2 := versions[rand.Intn(len(versions))]
		return fmt.Sprintf(selectedPattern.template, version, version2)
	default:
		return fmt.Sprintf(selectedPattern.template, version)
	}
}

// GenerateRandom 生成完全随机的UserAgent（不受任何限制）
func GenerateRandom() string {
	// 随机选择一个模式
	selectedPattern := userAgentPatterns[rand.Intn(len(userAgentPatterns))]

	// 根据浏览器类型获取版本号
	versions := browserVersions[selectedPattern.browser]
	if len(versions) == 0 {
		return ""
	}

	version := versions[rand.Intn(len(versions))]

	// 格式化UserAgent字符串
	switch selectedPattern.browser {
	case Firefox:
		// Firefox需要两个版本号
		version2 := versions[rand.Intn(len(versions))]
		return fmt.Sprintf(selectedPattern.template, version, version2)
	case Edge:
		// Edge需要两个版本号
		version2 := versions[rand.Intn(len(versions))]
		return fmt.Sprintf(selectedPattern.template, version, version2)
	default:
		return fmt.Sprintf(selectedPattern.template, version)
	}
}
